package com.hefei.garden.config.websocket;

import com.hefei.garden.config.websocket.handler.WebSocketHandler;
import com.hefei.garden.config.websocket.interceptor.WebSocketInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author: devil
 * @Date: 2022/7/28 21
 * @Description:
 */
@Configuration
@EnableWebSocket
public class WebsocketConfiguration implements WebSocketConfigurer {

    /**
     * 连接地址：ws://127.0.0.1:9906/websocket?user=yuxiu
     * 测试发送消息格式：{"content":"test", "toUser":"qianqian"}
     *
     * @param registry
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new WebSocketHandler(), "/websocket")
                .addInterceptors(new WebSocketInterceptor())
                .setAllowedOrigins("*");
        registry.addHandler(new WebSocketHandler(), "/sock-js")
                .addInterceptors(new WebSocketInterceptor())
                .setAllowedOrigins("*")
                .withSockJS();
    }

}

